package com.system.userserver.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.system.commonutils.bean.ApiResult;
import com.system.commonutils.bean.SecendMenu;
import com.system.commonutils.pojo.Menu;
import com.system.commonutils.pojo.Role;
import com.system.userserver.bean.MenuParams;
import com.system.userserver.bean.RoleMenusParams;
import com.system.userserver.bean.RoleParams;
import com.system.userserver.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;


@Api(tags = "用户服务-角色API")
@RestController
@Validated
@RequestMapping(value = "role")
@DefaultProperties(defaultFallback = "golbalFallback")
public class RoleController {

    @Autowired
    RoleService roleService;


    @HystrixCommand
    @ApiOperation(value = "获取所有角色")
    @GetMapping(value = "getRoleList",produces = "application/json;charset=UTF-8")
    public ApiResult getRoleList(){

        List<Role> roles = roleService.selectRoleList();
        return ApiResult.successWithData(roles);
    }

    @HystrixCommand
    @ApiOperation(value = "获取所有菜单")
    @GetMapping(value = "getMenuList",produces = "application/json;charset=UTF-8")
    public ApiResult getMenuList(){

        List<Menu> menus = roleService.selectMenuList();
        return ApiResult.successWithData(menus);
    }

    @HystrixCommand
    @ApiOperation(value = "根据角色获取菜单",httpMethod = "get")
    @GetMapping(value ="/getMenuListByRole",produces = "application/json;charset=utf-8")
    public ApiResult getMenuListByRole(
            @NotNull @RequestParam(required = false) Integer roleId){
        List<SecendMenu> allMenu = roleService.getMenuByRole(roleId);
        return ApiResult.successWithData("成功",allMenu);
    }

    @HystrixCommand
    @ApiOperation(value = "添加菜单")
    @PostMapping(value = "addMenu",produces = "application/json;charset=UTF-8")
    public ApiResult addMenu(@RequestBody MenuParams menuParams){
        Boolean aBoolean = roleService.addMenu(menuParams);
        return ApiResult.withBoolean(aBoolean,"添加成功","添加失败");
    }

    @HystrixCommand
    @ApiOperation(value = "批量添加菜单")
    @PostMapping(value = "addMenus",produces = "application/json;charset=UTF-8")
    public ApiResult addMenu(@RequestBody List<MenuParams> menuParams){
        Boolean aBoolean = roleService.addMenus(menuParams);
        return ApiResult.withBoolean(aBoolean,"添加成功","添加失败");
    }

    @HystrixCommand
    @ApiOperation(value = "添加角色")
    @PostMapping(value = "addRole",produces = "application/json;charset=UTF-8")
    public ApiResult addRole(@RequestBody RoleParams roleParams){
        Boolean aBoolean = roleService.addRole(roleParams);
        return ApiResult.withBoolean(aBoolean,"添加成功","添加失败");
    }


    @ApiOperation(value = "删除菜单")
    @PostMapping(value = "/deleteMenuById",produces = "application/json;charset=utf-8")
    public ApiResult deleteMenuById(@NotNull @RequestParam(required = false) Integer menuId){
        boolean b = roleService.deleteMenu(menuId);
        return ApiResult.withBoolean(b);
    }

    @ApiOperation(value = "后端删除角色",response = ApiResult.class)
    @PostMapping(value = "/deleteRoleById",produces = "application/json;charset=utf-8")
    public ApiResult deleteRoleById(
            @NotNull @RequestParam(required = false) Integer roleId){
        boolean b = roleService.deleteRoleById(roleId);
        return ApiResult.withBoolean(b);
    }

    @ApiOperation(value = "后端修改菜单",response = ApiResult.class)
    @PostMapping(value = "/changeMenu",produces = "application/json;charset=utf-8")
    public ApiResult changeMenu(
            @Validated @RequestBody
                    Menu menu){
        Boolean aBoolean = roleService.changeMenu(menu);
        return ApiResult.withBoolean(aBoolean);
    }

    @ApiOperation(value = "后端修改角色",response = ApiResult.class)
    @PostMapping(value = "/changRole",produces = "application/json;charset=utf-8")
    public ApiResult changRole(@Validated @RequestBody
                                       Role role){
        Boolean aBoolean = roleService.changRole(role);
        return ApiResult.withBoolean(aBoolean);
    }

    @ApiOperation(value = "角色批量删除菜单",response = ApiResult.class)
    @PostMapping(value = "/roleDeleteMenus",produces = "application/json;charset=utf-8")
    public ApiResult roleDeleteMenus(
            @RequestBody
            @ApiParam RoleMenusParams role){
        Boolean aBoolean = roleService.roleDeleteMenus(role);
        return ApiResult.withBoolean(aBoolean);
    }

    @ApiOperation(value = "角色批量添加菜单(后端已调)",response = ApiResult.class)
    @PostMapping(value = "/roleAddMenus",produces = "application/json;charset=utf-8")
    public ApiResult roleAddMenus(
            @RequestBody
            @ApiParam RoleMenusParams role){
        Boolean aBoolean = roleService.roleAddMenus(role);
        return ApiResult.withBoolean(aBoolean);
    }

    @ApiOperation(value = "用户修改角色")
    @PostMapping(value = "/userChangeRole",produces = "application/json;charset=utf-8")
    public ApiResult userChangeRole(Integer userId,Integer roleId){
        if(roleId==5){
            return ApiResult.failure("参数错误");
        }
        Boolean role = roleService.userChangeRole(userId, roleId);
        return ApiResult.withBoolean(role);
    }








    /**
     * 默认熔断回调
     * @return
     */
    public ApiResult golbalFallback(Throwable throwable){
        String localizedMessage = throwable.getLocalizedMessage();
        throwable.printStackTrace();
        return ApiResult.failure(localizedMessage);
    }
}
